package torres.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.List;

import javax.swing.JPanel;

import torres.entity.Entity;
import torres.entity.Merchant;
import torres.item.Inventory;
import torres.item.TakeableItem;
import torres.map.World;

@SuppressWarnings("serial")
public class ShopMenuView extends JPanel{

	int screenWidth ,x;
	int screenHeight, y;
	Font body;
	Font headers;
	Entity player;
	Merchant merchant;
	HashMap<Integer,Dimension> playerInventoryTable;
	HashMap<Integer,Dimension> shopInventoryTable;
	String extension = new String("_small.png");
	
	public ShopMenuView() {

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		screenWidth = screenSize.width;
		screenHeight = screenSize.height;
		
		x = 2*screenWidth/3;
		y = 2*screenHeight/3;
		
		body = new Font("Helvetica",Font.BOLD, 14);
		headers = new Font("Helvetica",Font.ITALIC, 18);
		
		setIgnoreRepaint(true);
		setPreferredSize(new Dimension(x,y));
		setLocation(screenWidth-x/2, screenHeight-y/2);
		setLayout(null);
		setVisible(true);
		setFocusable(true);
		setOpaque(true);
		
		player = World.getInstance().getPlayer();

	}
	
	public synchronized void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		Graphics2D g2 = (Graphics2D)g;
		
		g.setColor(Color.black);
		drawWindow(g);

		int yOffset = 35;
		int xOffset = 20;
		
		g.setColor(Color.white);
		g.setFont(headers);
		g.drawString("Player",screenWidth/2-x/2 + xOffset,screenHeight/2-y/2 + yOffset );
		g.drawString("Merchant", screenWidth/2 + xOffset, screenHeight/2-y/2 + yOffset);
		
		drawPlayerItems(g2);
		drawMerchantItems(g2);
		
	}
	
	public void drawPlayerItems(Graphics2D g2) {
		Inventory inventory = player.getInventory();
		List<TakeableItem> itemList = inventory.getItemList();
		
		int itemXSize;
		int itemYSize;
		
		if (!itemList.isEmpty()) {
			torres.item.Item itemSample = itemList.get(0);
			itemXSize = ImageLoader.loadImage(itemSample.toString() + extension).getWidth() + 30;
			itemYSize = ImageLoader.loadImage(itemSample.toString() + extension).getHeight() + 30;
		}
		else {
			itemXSize = 40 + 30;
			itemYSize = 40 + 30;
		}
		
		int xOffset = 25;
		int yOffset = 35;
		int ySpace = 20;
		
		g2.setColor(Color.darkGray);
		
		for (int i=0; i < inventory.getMaxSize()/4 ; i++) {
			
			g2.draw3DRect(screenWidth/2 - x/2 + xOffset,
					screenHeight/2 - y/2 + yOffset + ySpace + i*itemXSize,
					itemXSize + 2,
					itemYSize + 2,
					true);
			
			g2.draw3DRect(screenWidth/2 - x/8 + xOffset,
					screenHeight/2 - y/2 + yOffset + ySpace + i*itemXSize,
					itemXSize + 2,
					itemYSize + 2,
					true);
			
			g2.draw3DRect(screenWidth/2 - x/4 + xOffset,
					screenHeight/2 - y/2 + yOffset + ySpace + i*itemXSize,
					itemXSize + 2,
					itemYSize + 2,
					true);
			
			g2.draw3DRect(screenWidth/2 - 3*x/8 + xOffset,
					screenHeight/2 - y/2 + yOffset + ySpace + i*itemXSize,
					itemXSize + 2,
					itemYSize + 2,
					true);
			
			ySpace += 15;
			
		}
		int i = 0;
		int index = 1;
		int col = 0;
		ySpace = 20;
		playerInventoryTable = new HashMap<Integer,Dimension>();
		
		for (TakeableItem item: itemList) {
			
			int xt;
			int yt;
			
			itemXSize = ImageLoader.loadImage(item.toString() + extension).getWidth() + 30;
			itemYSize = ImageLoader.loadImage(item.toString() + extension).getHeight() + 30;
			
			if(col == 0 || col == 4 || col == 8 || col == 12 || col == 16 || col == 20) {
				xt = screenWidth/2 - x/2 + xOffset + 2;
				yt = screenHeight/2 - y/2 + yOffset + ySpace + i*itemXSize + 2;
				g2.drawImage(ImageLoader.loadImage(item.toString() + extension),
					xt,yt,itemXSize,itemYSize,null);
				col++;
				playerInventoryTable.put(index++, new Dimension(xt,yt));
			}
			else if(col == 1 || col == 5 || col == 9 || col == 13 || col == 17 || col == 21) {
				xt = screenWidth/2 - 3*x/8 + xOffset + 2;
				yt = screenHeight/2 - y/2 + yOffset + ySpace + i*itemXSize + 2;
				g2.drawImage(ImageLoader.loadImage(item.toString() + extension),
						xt,yt,itemXSize + 2,itemYSize + 2,null);
				col++;
				playerInventoryTable.put(index++, new Dimension(xt,yt));
			}
			else if(col == 2 || col == 6 || col == 10 || col == 14 || col == 18 || col == 22) {
				xt = screenWidth/2 - x/4 + xOffset + 2;
				yt = screenHeight/2 - y/2 + yOffset + ySpace + i*itemXSize + 2;
				g2.drawImage(ImageLoader.loadImage(item.toString() + extension),
						xt,yt,itemXSize + 2,itemYSize + 2,null);
				col++;
				playerInventoryTable.put(index++,new Dimension(xt,yt));
			}
			else if(col == 3 || col == 7 || col == 11 || col == 15 || col == 19 || col == 23) {
				xt = screenWidth/2 - x/8 + xOffset + 2;
				yt = screenHeight/2 - y/2 + yOffset + ySpace + i*itemXSize;
				g2.drawImage(ImageLoader.loadImage(item.toString() + extension),
						xt,yt,itemXSize + 2,itemYSize + 2,null);
				playerInventoryTable.put(index++,new Dimension(xt,yt));
				col++;
				i++;
				ySpace += 15;
			}
		}
		
	}
	
	public void drawMerchantItems(Graphics2D g2) {
		List<TakeableItem> inventory = (List<TakeableItem>) merchant.getShopList();
		
		int itemXSize;
		int itemYSize;
		
		if (!inventory.isEmpty()) {
			torres.item.Item itemSample = inventory.get(0);
			itemXSize = ImageLoader.loadImage(itemSample.toString() + extension).getWidth() + 30;
			itemYSize = ImageLoader.loadImage(itemSample.toString() + extension).getHeight() + 30;
		}
		else {
			itemXSize = 40 + 30;
			itemYSize = 40 + 30;
		}
		
		int xOffset = 25;
		int yOffset = 35;
		int ySpace = 20;
		
		g2.setColor(Color.darkGray);
		
		for (int i=0; i < 5 ; i++) {
			
			g2.draw3DRect(screenWidth/2 + xOffset,
					screenHeight/2 - y/2 + yOffset + ySpace + i*itemXSize,
					itemXSize + 2,
					itemYSize + 2,
					true);
			
			g2.draw3DRect(screenWidth/2 + x/8 + xOffset,
					screenHeight/2 - y/2 + yOffset + ySpace + i*itemXSize,
					itemXSize + 2,
					itemYSize + 2,
					true);
			
			g2.draw3DRect(screenWidth/2 + x/4 + xOffset,
					screenHeight/2 - y/2 + yOffset + ySpace + i*itemXSize,
					itemXSize + 2,
					itemYSize + 2,
					true);
			
			g2.draw3DRect(screenWidth/2 + 3*x/8 + xOffset,
					screenHeight/2 - y/2 + yOffset + ySpace + i*itemXSize,
					itemXSize + 2,
					itemYSize + 2,
					true);
			
			ySpace += 15;
			
		}
		int i = 0;
		int index = 1;
		int col = 0;
		ySpace = 20;
		shopInventoryTable = new HashMap<Integer,Dimension>();
		
		for (TakeableItem item: inventory) {
			
			int xt;
			int yt;
			
			itemXSize = ImageLoader.loadImage(item.toString() + extension).getWidth() + 30;
			itemYSize = ImageLoader.loadImage(item.toString() + extension).getHeight() + 30;
			
			if(col == 0 || col == 4 || col == 8 || col == 12 || col == 16 || col == 20) {
				xt = screenWidth/2 + xOffset + 2;
				yt = screenHeight/2 - y/2 + yOffset + ySpace + i*itemXSize + 2;
				g2.drawImage(ImageLoader.loadImage(item.toString() + extension),
					xt,yt,itemXSize,itemYSize,null);
				col++;
				shopInventoryTable.put(index++, new Dimension(xt,yt));
			}
			else if(col == 1 || col == 5 || col == 9 || col == 13 || col == 17 || col == 21) {
				xt = screenWidth/2 + x/8 + xOffset;
				yt = screenHeight/2 - y/2 + yOffset + ySpace + i*itemXSize;
				g2.drawImage(ImageLoader.loadImage(item.toString() + extension),
						xt,yt,itemXSize + 2,itemYSize + 2,null);
				col++;
				shopInventoryTable.put(index++, new Dimension(xt,yt));
			}
			else if(col == 2 || col == 6 || col == 10 || col == 14 || col == 18 || col == 22) {
				xt = screenWidth/2 + x/4 + xOffset;
				yt = screenHeight/2 - y/2 + yOffset + ySpace + i*itemXSize;
				g2.drawImage(ImageLoader.loadImage(item.toString() + extension),
						xt,yt,itemXSize + 2,itemYSize + 2,null);
				col++;
				shopInventoryTable.put(index++,new Dimension(xt,yt));
			}
			else if(col == 3 || col == 7 || col == 11 || col == 15 || col == 19 || col == 23) {
				xt = screenWidth/2 + 3*x/8 + xOffset;
				yt = screenHeight/2 - y/2 + yOffset + ySpace + i*itemXSize;
				g2.drawImage(ImageLoader.loadImage(item.toString() + extension),
						xt,yt,itemXSize + 2,itemYSize + 2,null);
				shopInventoryTable.put(index++,new Dimension(xt,yt));
				col++;
				i++;
				ySpace += 15;
			}
		}
		
	}
	
	public void drawWindow(Graphics g) {
		ImageLoader loader = new ImageLoader("texture.jpg");
		BufferedImage img = loader.getImage();
		g.drawImage(img, screenWidth/2-x/2,screenHeight/2-y/2, x, y, null);
	}
	
	public void update(Graphics g) {
		update(g, merchant);
	}
	
	public void update(Graphics g, Merchant m) {
		merchant = m;
		paintComponent(g);
	}
	
	public int getScreenWidth() {
		return screenWidth;
	}

	public int getX() {
		return x;
	}

	public int getScreenHeight() {
		return screenHeight;
	}

	public int getY() {
		return y;
	}
	public HashMap<Integer,Dimension> getPlayerInventoryTable() {
		return playerInventoryTable;
	}
	public HashMap<Integer,Dimension> getMerchantInventoryTable() {
		return shopInventoryTable;
	}
}
